home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 4 / Macwelt DVD 4.cdr / Entwickler / Mac-OS / oxygen / samples / docbook / xsl / lib / lib.xsl < prev   
Encoding:
Extensible Markup Language  |  2002-09-09  |  15.0 KB  |  400 lines  |  [□□□□/□□□□]

  1. lib.xsl9°π¢tÚπ¢tÚÅÅÌé<?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.                 xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
  4.                 exclude-result-prefixes="doc"
  5.                 version='1.0'>
  6.  
  7. <!-- ********************************************************************
  8.      $Id: lib.xsl,v 1.1 2002/05/29 09:55:13 dan Exp $
  9.      ********************************************************************
  10.  
  11.      This file is part of the XSL DocBook Stylesheet distribution.
  12.      See ../README or http://nwalsh.com/docbook/xsl/ for copyright
  13.      and other information.
  14.  
  15.      This module implements DTD-independent functions
  16.  
  17.      ******************************************************************** -->
  18.  
  19. <doc:reference xmlns="">
  20. <referenceinfo>
  21. <releaseinfo role="meta">
  22. $Id: lib.xsl,v 1.1 2002/05/29 09:55:13 dan Exp $
  23. </releaseinfo>
  24. <author><surname>Walsh</surname>
  25. <firstname>Norman</firstname></author>
  26. <copyright><year>1999</year><year>2000</year>
  27. <holder>Norman Walsh</holder>
  28. </copyright>
  29. </referenceinfo>
  30. <title>Library Template Reference</title>
  31.  
  32. <partintro>
  33. <section><title>Introduction</title>
  34.  
  35. <para>This is technical reference documentation for the DocBook XSL
  36. Stylesheets; it documents (some of) the parameters, templates, and
  37. other elements of the stylesheets.</para>
  38.  
  39. <para>This is not intended to be <quote>user</quote> documentation.
  40. It is provided for developers writing customization layers for the
  41. stylesheets, and for anyone who's interested in <quote>how it
  42. works</quote>.</para>
  43.  
  44. <para>Although I am trying to be thorough, this documentation is known
  45. to be incomplete. Don't forget to read the source, too :-)</para>
  46. </section>
  47. </partintro>
  48. </doc:reference>
  49.  
  50. <xsl:template name="dot.count">
  51.   <!-- Returns the number of "." characters in a string -->
  52.   <xsl:param name="string"></xsl:param>
  53.   <xsl:param name="count" select="0"/>
  54.   <xsl:choose>
  55.     <xsl:when test="contains($string, '.')">
  56.       <xsl:call-template name="dot.count">
  57.         <xsl:with-param name="string" select="substring-after($string, '.')"/>
  58.         <xsl:with-param name="count" select="$count+1"/>
  59.       </xsl:call-template>
  60.     </xsl:when>
  61.     <xsl:otherwise>
  62.       <xsl:value-of select="$count"/>
  63.     </xsl:otherwise>
  64.   </xsl:choose>
  65. </xsl:template>
  66.  
  67. <!-- ================================================================== -->
  68.  
  69. <xsl:template name="copy-string">
  70.   <!-- returns 'count' copies of 'string' -->
  71.   <xsl:param name="string"></xsl:param>
  72.   <xsl:param name="count" select="0"/>
  73.   <xsl:param name="result"></xsl:param>
  74.  
  75.   <xsl:choose>
  76.     <xsl:when test="$count>0">
  77.       <xsl:call-template name="copy-string">
  78.         <xsl:with-param name="string" select="$string"/>
  79.         <xsl:with-param name="count" select="$count - 1"/>
  80.         <xsl:with-param name="result">
  81.           <xsl:value-of select="$result"/>
  82.           <xsl:value-of select="$string"/>
  83.         </xsl:with-param>
  84.       </xsl:call-template>
  85.     </xsl:when>
  86.     <xsl:otherwise>
  87.       <xsl:value-of select="$result"/>
  88.     </xsl:otherwise>
  89.   </xsl:choose>
  90. </xsl:template>
  91.  
  92. <!-- ====================================================================== -->
  93.  
  94. <doc:template name="string.subst" xmlns="">
  95. <refpurpose>Substitute one text string for another in a string</refpurpose>
  96. <refdescription>
  97. <para>The <function>string.subst</function> template replaces all
  98. occurances of <parameter>target</parameter> in <parameter>string</parameter>
  99. with <parameter>replacement</parameter> and returns the result.
  100. </para>
  101. </refdescription>
  102. </doc:template>
  103.  
  104. <xsl:template name="string.subst">
  105.   <xsl:param name="string"></xsl:param>
  106.   <xsl:param name="target"></xsl:param>
  107.   <xsl:param name="replacement"></xsl:param>
  108.  
  109.   <xsl:choose>
  110.     <xsl:when test="contains($string, $target)">
  111.       <xsl:variable name="rest">
  112.         <xsl:call-template name="string.subst">
  113.           <xsl:with-param name="string"
  114.                           select="substring-after($string, $target)"/>
  115.           <xsl:with-param name="target" select="$target"/>
  116.           <xsl:with-param name="replacement" select="$replacement"/>
  117.         </xsl:call-template>
  118.       </xsl:variable>
  119.       <xsl:value-of select="concat(substring-before($string, $target),
  120.                                    $replacement,
  121.                                    $rest)"/>
  122.     </xsl:when>
  123.     <xsl:otherwise>
  124.       <xsl:value-of select="$string"/>
  125.     </xsl:otherwise>
  126.   </xsl:choose>
  127. </xsl:template>
  128.  
  129. <!-- ================================================================== -->
  130.  
  131. <doc:template name="xpointer.idref" xmlns="">
  132. <refpurpose>Extract IDREF from an XPointer</refpurpose>
  133. <refdescription>
  134. <para>The <function>xpointer.idref</function> template returns the
  135. ID portion of an XPointer which is a pointer to an ID within the current
  136. document, or the empty string if it is not.</para>
  137. <para>In other words, <function>xpointer.idref</function> returns
  138. <quote>foo</quote> when passed either <literal>#foo</literal>
  139. or <literal>#xpointer(id('foo'))</literal>, otherwise it returns
  140. the empty string.</para>
  141. </refdescription>
  142. </doc:template>
  143.  
  144. <xsl:template name="xpointer.idref">
  145.   <xsl:param name="xpointer">http://...</xsl:param>
  146.   <xsl:choose>
  147.     <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
  148.       <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"/>
  149.       <xsl:variable name="quote" select="substring($rest, 1, 1)"/>
  150.       <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"/>
  151.     </xsl:when>
  152.     <xsl:when test="starts-with($xpointer, '#')">
  153.       <xsl:value-of select="substring-after($xpointer, '#')"/>
  154.     </xsl:when>
  155.     <!-- otherwise it's a pointer to some other document -->
  156.   </xsl:choose>
  157. </xsl:template>
  158.  
  159. <!-- ================================================================== -->
  160.  
  161. <doc:template name="length-magnitude" xmlns="">
  162. <refpurpose>Return the unqualified dimension from a length specification</refpurpose>
  163. <refdescription>
  164. <para>The <function>length-magnitude</function> template returns the
  165. unqualified length ("20" for "20pt") from a dimension.
  166. </para>
  167. </refdescription>
  168. </doc:template>
  169.  
  170. <xsl:template name="length-magnitude">
  171.   <xsl:param name="length" select="'0pt'"/>
  172.  
  173.   <xsl:choose>
  174.     <xsl:when test="string-length($length) = 0"/>
  175.     <xsl:when test="substring($length,1,1) = '0'
  176.                     or substring($length,1,1) = '1'
  177.                     or substring($length,1,1) = '2'
  178.                     or substring($length,1,1) = '3'
  179.                     or substring($length,1,1) = '4'
  180.                     or substring($length,1,1) = '5'
  181.                     or substring($length,1,1) = '6'
  182.                     or substring($length,1,1) = '7'
  183.                     or substring($length,1,1) = '8'
  184.                     or substring($length,1,1) = '9'
  185.                     or substring($length,1,1) = '.'">
  186.       <xsl:value-of select="substring($length,1,1)"/>
  187.       <xsl:call-template name="length-magnitude">
  188.         <xsl:with-param name="length" select="substring($length,2)"/>
  189.       </xsl:call-template>
  190.     </xsl:when>
  191.   </xsl:choose>
  192. </xsl:template>
  193.  
  194. <!-- ================================================================== -->
  195.  
  196. <doc:template name="length-units" xmlns="">
  197. <refpurpose>Return the units from a length specification</refpurpose>
  198. <refdescription>
  199. <para>The <function>length-units</function> template returns the
  200. units ("pt" for "20pt") from a length. If no units are supplied on the
  201. length, the <parameter>defauilt.units</parameter> are returned.</para>
  202. </refdescription>
  203. </doc:template>
  204.  
  205. <xsl:template name="length-units">
  206.   <xsl:param name="length" select="'0pt'"/>
  207.   <xsl:param name="default.units" select="'pt'"/>
  208.   <xsl:variable name="magnitude">
  209.     <xsl:call-template name="length-magnitude">
  210.       <xsl:with-param name="length" select="$length"/>
  211.     </xsl:call-template>
  212.   </xsl:variable>
  213.   <xsl:variable name="units">
  214.     <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
  215.   </xsl:variable>
  216.  
  217.   <xsl:choose>
  218.     <xsl:when test="$units = ''">
  219.       <xsl:value-of select="$default.units"/>
  220.     </xsl:when>
  221.     <xsl:otherwise>
  222.       <xsl:value-of select="$units"/>
  223.     </xsl:otherwise>
  224.   </xsl:choose>
  225. </xsl:template>
  226.  
  227. <!-- ================================================================== -->
  228.  
  229. <doc:template name="length-spec" xmlns="">
  230. <refpurpose>Return a fully qualified length specification</refpurpose>
  231. <refdescription>
  232. <para>The <function>length-spec</function> template returns the
  233. qualified length from a dimension. If an unqualified length is given,
  234. the <parameter>default.units</parameter> will be added to it.
  235. </para>
  236. </refdescription>
  237. </doc:template>
  238.  
  239. <xsl:template name="length-spec">
  240.   <xsl:param name="length" select="'0pt'"/>
  241.   <xsl:param name="default.units" select="'pt'"/>
  242.   <xsl:variable name="magnitude">
  243.     <xsl:call-template name="length-magnitude">
  244.       <xsl:with-param name="length" select="$length"/>
  245.     </xsl:call-template>
  246.   </xsl:variable>
  247.   <xsl:variable name="units">
  248.     <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
  249.   </xsl:variable>
  250.  
  251.   <xsl:value-of select="$magnitude"/>
  252.   <xsl:choose>
  253.     <xsl:when test="$units='cm'
  254.                     or $units='mm'
  255.                     or $units='in'
  256.                     or $units='pt'
  257.                     or $units='pc'
  258.                     or $units='px'
  259.                     or $units='em'">
  260.       <xsl:value-of select="$units"/>
  261.     </xsl:when>
  262.     <xsl:when test="$units = ''">
  263.       <xsl:value-of select="$default.units"/>
  264.     </xsl:when>
  265.     <xsl:otherwise>
  266.       <xsl:message>
  267.         <xsl:text>Unrecognized unit of measure: </xsl:text>
  268.         <xsl:value-of select="$units"/>
  269.         <xsl:text>.</xsl:text>
  270.       </xsl:message>
  271.     </xsl:otherwise>
  272.   </xsl:choose>
  273. </xsl:template>
  274.  
  275. <!-- ================================================================== -->
  276.  
  277. <doc:template name="pi-attribute" xmlns="">
  278. <refpurpose>Extract a pseudo-attribute from a PI</refpurpose>
  279. <refdescription>
  280. <para>The <function>pi-attribute</function> template extracts a pseudo-attribute
  281. from a processing instruction. For example, given the PI
  282. <quote><literal><?foo bar="1" baz='red'?></literal></quote>,</para>
  283. <programlisting><![CDATA[<xsl:call-template name="pi-attribute">
  284.   <xsl:with-param name="pis" select="processing-instruction('foo')"/>
  285.   <xsl:with-param name="attribute" select="'baz'"/>
  286. </xsl:call-template>]]></programlisting>
  287. <para>will return <quote>red</quote>. This template returns the first matching
  288. attribute that it finds. Presented with processing instructions that
  289. contain badly formed pseudo-attributes (missing or unbalanced quotes,
  290. for example), the template may silently return erroneous results.</para>
  291. </refdescription>
  292. </doc:template>
  293.  
  294. <xsl:template name="pi-attribute">
  295.   <xsl:param name="pis" select="processing-instruction('')"/>
  296.   <xsl:param name="attribute">filename</xsl:param>
  297.   <xsl:param name="count">1</xsl:param>
  298.  
  299.   <xsl:choose>
  300.     <xsl:when test="$count>count($pis)">
  301.       <!-- not found -->
  302.     </xsl:when>
  303.     <xsl:otherwise>
  304.       <xsl:variable name="pi">
  305.         <xsl:value-of select="$pis[$count]"/>
  306.       </xsl:variable>
  307.       <xsl:choose>
  308.         <xsl:when test="contains($pi,concat($attribute, '='))">
  309.           <xsl:variable name="rest" select="substring-after($pi,concat($attribute,'='))"/>
  310.           <xsl:variable name="quote" select="substring($rest,1,1)"/>
  311.           <xsl:value-of select="substring-before(substring($rest,2),$quote)"/>
  312.         </xsl:when>
  313.         <xsl:otherwise>
  314.           <xsl:call-template name="pi-attribute">
  315.             <xsl:with-param name="pis" select="$pis"/>
  316.             <xsl:with-param name="attribute" select="$attribute"/>
  317.             <xsl:with-param name="count" select="$count + 1"/>
  318.           </xsl:call-template>
  319.         </xsl:otherwise>
  320.       </xsl:choose>
  321.     </xsl:otherwise>
  322.   </xsl:choose>
  323. </xsl:template>
  324.  
  325. <!-- ================================================================== -->
  326.  
  327. <doc:template name="lookup.key" xmlns="">
  328. <refpurpose>Retrieve the value associated with a particular key in a table</refpurpose>
  329. <refdescription>
  330. <para>Given a table of space-delimited key/value pairs,
  331. the <function>lookup.key</function> template extracts the value associated
  332. with a particular key.</para>
  333. </refdescription>
  334. </doc:template>
  335.  
  336. <xsl:template name="lookup.key">
  337.   <xsl:param name="key" select="''"/>
  338.   <xsl:param name="table" select="''"/>
  339.  
  340.   <xsl:if test="contains($table, ' ')">
  341.     <xsl:choose>
  342.       <xsl:when test="substring-before($table, ' ') = $key">
  343.         <xsl:variable name="rest" select="substring-after($table, ' ')"/>
  344.         <xsl:choose>
  345.           <xsl:when test="contains($rest, ' ')">
  346.             <xsl:value-of select="substring-before($rest, ' ')"/>
  347.           </xsl:when>
  348.           <xsl:otherwise>
  349.             <xsl:value-of select="$rest"/>
  350.           </xsl:otherwise>
  351.         </xsl:choose>
  352.       </xsl:when>
  353.       <xsl:otherwise>
  354.         <xsl:call-template name="lookup.key">
  355.           <xsl:with-param name="key" select="$key"/>
  356.           <xsl:with-param name="table"
  357.                             select="substring-after(substring-after($table,' '), ' ')"/>
  358.         </xsl:call-template>
  359.       </xsl:otherwise>
  360.     </xsl:choose>
  361.   </xsl:if>
  362. </xsl:template>
  363.  
  364. <!-- ================================================================== -->
  365.  
  366. <doc:template name="xpath.location" xmlns="">
  367. <refpurpose>Calculate the XPath child-sequence to the current node</refpurpose>
  368. <refdescription>
  369. <para>The <function>xpath.location</function> template calculates the
  370. absolute path from the root of the tree to the current element node.
  371. </para>
  372. </refdescription>
  373. </doc:template>
  374.  
  375. <xsl:template name="xpath.location">
  376.   <xsl:param name="node" select="."/>
  377.   <xsl:param name="path" select="''"/>
  378.  
  379.   <xsl:variable name="next.path">
  380.     <xsl:value-of select="local-name($node)"/>
  381.     <xsl:if test="$path != ''">/</xsl:if>
  382.     <xsl:value-of select="$path"/>
  383.   </xsl:variable>
  384.  
  385.   <xsl:choose>
  386.     <xsl:when test="$node/parent::*">
  387.       <xsl:call-template name="xpath.location">
  388.         <xsl:with-param name="node" select="$node/parent::*"/>
  389.         <xsl:with-param name="path" select="$next.path"/>
  390.       </xsl:call-template>
  391.     </xsl:when>
  392.     <xsl:otherwise>
  393.       <xsl:text>/</xsl:text>
  394.       <xsl:value-of select="$next.path"/>
  395.     </xsl:otherwise>
  396.   </xsl:choose>
  397. </xsl:template>
  398.  
  399. </xsl:stylesheet>
  400. This resource fork intentionally left blank   ˇˇ